#include <avr/wdt.h>
#include <Timer.h>
#include "Wire.h"
#include <SD.h>
#include <Wstring.h>

#define DELAY1S     1000
#define DELAY5S     5000
#define MAX         4090
#define MIN         0
#define adres       4 //I2C adres
#define DEBUG

File myFile;
String buf;
char character;
int i;
const int chipSelect = 10;
int settings[4];

void setup()
{
  Serial.begin(9600);
  Serial.println("Starting...");
  Wire.begin(4);
  pinMode(10, OUTPUT);
  if (!SD.begin(6)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
  getSettings();
}  
void getSettings()
{
  myFile = SD.open("arduino.csv");
  while (myFile.available()) {
      character = myFile.read();
      if (character == ','){
          settings[i]=buf.toFloat();
          i++;
          buf="";
          character=NULL;
        }
       else if (character == '\n'){
         Serial.println ("verzenden gegevens...");
         Serial.print ("inputnode = ");
         Serial.println (settings[0]);
         Serial.print ("inputpin = ");
         Serial.println (settings[1]);
         Serial.print ("outputnode = ");
         Serial.println (settings[2]);
         Serial.print ("outputpin = ");
         Serial.println (settings[3]);
         Serial.print ("type = ");
         Serial.println (settings[4]);
         sendconfig (settings[0],settings[1],settings[2],settings[3],settings[4]);
         i=0;
         character = NULL;
       }
       else {
           buf.concat(character);
           character=NULL;         
         }  
       }
      
   myFile.close();
} 
void sendconfig(byte inputnode,byte inputpin,byte outputnode,byte outputpin,byte type)
{
  Wire.beginTransmission(inputnode); // transmit to device #4
  Wire.write(inputpin);        // sends five bytes
  Wire.write(outputnode);   // sends one byte  
  Wire.write(outputpin);
  Wire.write(type);
  Wire.endTransmission();    // stop transmitting
 }
  
void loop()
{
	// nothing happens after setup
sendconfig(8,1,2,3,4);
delay(100);
}
